package com.pureut.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.quality.domain.QualityCheckDoc;
import com.pureut.quality.domain.dto.QualityCheckDocDto;
import com.pureut.quality.domain.vo.QualityCheckDocVo;
import com.pureut.quality.mapper.QualityCheckDocMapper;
import com.pureut.quality.service.IQualityCheckDocService;
import com.pureut.quality.service.QualityBadSampleService;
import com.pureut.quality.service.QualitySampleInformationService;
import com.pureut.quality.service.QualityTemporaryService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 检验单据配置实现类
 * @Param
 * @Date 23-02-02
 * @Author hl
 **/
@Service
public class QualityCheckDocServiceImpl extends ServiceImpl<QualityCheckDocMapper, QualityCheckDoc> implements IQualityCheckDocService {

    @Resource
    private QualityCheckDocMapper qualityCheckDocMapper;

    @Resource
    private QualityTemporaryService qualityTemporaryService;

    @Resource
    private QualityBadSampleService qualityBadSampleService;

    @Resource
    private QualitySampleInformationService qualitySampleInformationService;

    /**
     * 查询检验单据配置
     *
     * @param qualityCheckDocVo 检验单据配置接受类
     * @return List
     */
    @Override
    @DataScope(deptAlias = "quality_check_doc")
    public List<QualityCheckDocDto> selectCheckDocList(QualityCheckDocVo qualityCheckDocVo) {
        List<QualityCheckDocDto> list = qualityCheckDocMapper.selectCheckDocList(qualityCheckDocVo);

        //检测阶别
        List<SysDictData> rankArray = DictUtils.getDictCache("item_rank");
        Map<String, String> rankMap = rankArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //物料类别
        List<SysDictData> materialCategoryArray = DictUtils.getDictCache("quality_category");
        Map<String, String> materialCategoryMap = materialCategoryArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //判定方式
        List<SysDictData> decisionModeArray = DictUtils.getDictCache("quality_check_doc_decision_mode");
        Map<String, String> decisionModeMap = decisionModeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        for (QualityCheckDocDto entity : list) {
            //检测阶别
            entity.setItemRankDict(rankMap.get(entity.getItemRank()));
            //类别
            entity.setMaterialTypeDict(materialCategoryMap.get(entity.getMaterialType()));
            //判定方式
            entity.setCheckDecisionModeDict(decisionModeMap.get(entity.getCheckDecisionMode()));
        }
        return list;
    }

    /**
     * 新增检验单据配置
     *
     * @param qualityCheckDocVo 检验单据配置接受类
     * @return int 1 成功 0失败
     */
    @Override
    public boolean insertCheckDoc(QualityCheckDocVo qualityCheckDocVo) {
        if (qualityCheckDocVo.getCheckDefaultMark() == 1) {
            QualityCheckDoc qualityCheckDoc = qualityCheckDocMapper.selectOne(new QueryWrapper<QualityCheckDoc>().lambda().eq(QualityCheckDoc::getCheckDefaultMark, 1));
            if (qualityCheckDoc != null) {
                throw new GlobalException("只能存在一条默认数据");
            }
        }
        //校验同阶别同类别下只能有一条数据
        QualityCheckDoc qualityCheckDoc1 = qualityCheckDocMapper.selectOne(new QueryWrapper<QualityCheckDoc>().lambda().eq(QualityCheckDoc::getItemRank, qualityCheckDocVo.getItemRank()).eq(QualityCheckDoc::getMaterialType, qualityCheckDocVo.getMaterialType()));
        if (qualityCheckDoc1 != null) {
            throw new GlobalException("同阶别同类别的数据只能存在一条，请核实");
        }
        QualityCheckDoc qualityCheckDoc = new QualityCheckDoc();
        qualityCheckDoc.setItemRank(qualityCheckDocVo.getItemRank())
                .setMaterialType(qualityCheckDocVo.getMaterialType())
                .setCheckDecisionMode(qualityCheckDocVo.getCheckDecisionMode())
                .setCheckDecisionBasis(qualityCheckDocVo.getCheckDecisionBasis())
                .setCheckDefaultMark(qualityCheckDocVo.getCheckDefaultMark())
                .setCheckAutomaticAudit(qualityCheckDocVo.getCheckAutomaticAudit())
                .setCreateTime(new Date())
                .setDeptId(SecurityUtils.getDeptId());
        return save(qualityCheckDoc);
    }

    /**
     * 批量删除检验单据配置
     *
     * @param checkId id
     * @return int 1 成功 0失败
     */
    @Override
    public int deleteCheckDocById(String checkId) {
        String[] checkIdS = checkId.split(",");
        return qualityCheckDocMapper.deleteCheckDocById(checkIdS);
    }

    /**
     * 查询单个检验单据配置
     *
     * @param checkId id
     * @return QualityCheckDocDto 检验单据配置返回类
     */
    @Override
    public QualityCheckDocDto selectCheckDocById(long checkId) {
        QualityCheckDocVo vo = new QualityCheckDocVo();
        vo.setCheckId(checkId);
        List<QualityCheckDocDto> list = qualityCheckDocMapper.selectCheckDocList(vo);
        //检测阶别
        List<SysDictData> rankArray = DictUtils.getDictCache("item_rank");
        Map<String, String> rankMap = rankArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //物料类别
        List<SysDictData> materialCategoryArray = DictUtils.getDictCache("quality_category");
        Map<String, String> materialCategoryMap = materialCategoryArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //判定方式
        List<SysDictData> decisionModeArray = DictUtils.getDictCache("quality_check_doc_decision_mode");
        Map<String, String> decisionModeMap = decisionModeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        QualityCheckDocDto qualityCheckDocDto = list.get(0);
        //检测阶别
        qualityCheckDocDto.setItemRankDict(rankMap.get(qualityCheckDocDto.getItemRank()));
        //类别
        qualityCheckDocDto.setMaterialTypeDict(materialCategoryMap.get(qualityCheckDocDto.getMaterialType()));
        //判定方式
        qualityCheckDocDto.setCheckDecisionModeDict(decisionModeMap.get(qualityCheckDocDto.getCheckDecisionMode()));
        return qualityCheckDocDto;
    }

    /**
     * 修改检验单据配置
     *
     * @param qualityCheckDocVo 检验单据配置接受类
     * @return int 1 成功 0 失败
     */
    @Override
    public boolean updateCheckDocEdit(QualityCheckDocVo qualityCheckDocVo) {
        QualityCheckDoc byId = getById(qualityCheckDocVo.getCheckId());

        if (qualityCheckDocVo.getCheckDefaultMark() == 1) {
            List<QualityCheckDoc> qualityCheckDoc = qualityCheckDocMapper.selectList(new QueryWrapper<QualityCheckDoc>().lambda().eq(QualityCheckDoc::getCheckDefaultMark, 1));
            if (qualityCheckDoc.size() > 1) {
                throw new GlobalException("只能存在一条默认数据");
            }
        }
        //校验同阶别同类别下只能有一条数据
        List<QualityCheckDoc> qualityCheckDoc1 = qualityCheckDocMapper.selectList(new QueryWrapper<QualityCheckDoc>().lambda().eq(QualityCheckDoc::getItemRank, qualityCheckDocVo.getItemRank()).eq(QualityCheckDoc::getMaterialType, qualityCheckDocVo.getMaterialType()));
        if (qualityCheckDoc1.size() > 1) {
            throw new GlobalException("同阶别同类别的数据只能存在一条，请核实");
        }

        byId.setItemRank(qualityCheckDocVo.getItemRank())
                .setMaterialType(qualityCheckDocVo.getMaterialType())
                .setCheckDecisionMode(qualityCheckDocVo.getCheckDecisionMode())
                .setCheckDecisionBasis(qualityCheckDocVo.getCheckDecisionBasis())
                .setCheckDefaultMark(qualityCheckDocVo.getCheckDefaultMark())
                .setCheckAutomaticAudit(qualityCheckDocVo.getCheckAutomaticAudit());
        return updateById(byId);
    }
}
